// pagesShouYe/pages/searchAll/index.js
const app = getApp();
import { getStorage } from "../../../utils/localStorage";
import { baseUrl } from "../../../api/request";
import { listSeachAll } from "../../../api/homePage"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //自定义组件参数
    navData: {
      level: 2,
      title: '搜索'
    },
    //图片地址
    imgUrl: undefined,
    //当前位置经纬度
    latLng: undefined,
    //自定义导航栏高度
    pageTop: app.globalData.navHeight + app.globalData.navTop,
    //类型数据
    typeList: [
      {
        label: "综合",
        value: "0"
      },
      {
        label: "路线",
        value: "1"
      },
      {
        label: "活动",
        value: "2"
      },
      {
        label: "乡镇",
        value: "3"
      },
      {
        label: "乡村",
        value: "4"
      },
      {
        label: "景点",
        value: "5"
      },
      {
        label: "动态",
        value: "6"
      }
    ],
    typeIndex: 0,
    //搜索条件
    listQuery: {
      content: "",
      searchLabelId: 0
    },
    total: 0,
    //列表数据
    list: [],
    focusStatus:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      imgUrl: baseUrl,
      latLng: getStorage("latLng"),
    });
    if(options.key){
      this.setData({
        ['listQuery.content']:options.key,
      })
    }else{
      this.setData({
        focusStatus:true
      })
    }
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  //查询列表数据
  getList() {
    const _this = this.data;
    listSeachAll(_this.listQuery).then(res => {
      let arr = res.data.data.map(item => ({
        ...item,
        image: item.image ? item.image.split(",") : []
      }));
      this.setData({
        list: arr,
      });
    });
  },

  //列表点击事件
  listTap(e) {
    const item = e.currentTarget.dataset.item;
    switch (item.searchLabelId) {
      case "1":
        wx.navigateTo({
          url: '/pagesLuxian/pages/detail/index?id=' + item.id
        });
        break;
      case "2":
        wx.navigateTo({
          url: '/pagesShouYe/pages/bnnerDetailMore/index?id=' + item.id,
        });
        break;
      case "3":
        wx.navigateTo({
          url: '/pagesShouYe/pages/xiangzhenDetail/index?id=' + item.id,
        });
        break;
      case "4":
        wx.navigateTo({
          url: '/pagesShouYe/pages/xaingcunDetail/index?id=' + item.id,
        });
        break;
      case "5":
        wx.navigateTo({
          url: '/pagesShouYe/pages/jingquDetail/index?id=' + item.id,
        });
        break;
      case "6":
        wx.navigateTo({
          url: '/pagesShouYe/pages/listDetail/index?id=' + item.id,
        });
        break;

    };
  },

  /**  搜索框input bindblur事件 */
  searchBlur(e) {
    const value = e.detail.value;
    if (value) {
      this.setData({
        "listQuery.content": value,
        list: []
      });
      this.getList();
    } else {
      wx.showToast({
        icon: 'none',
        mask: true,
        title: '请输入您要搜索的内容',
        duration: 2000
      });
    };
  },

  /** 搜索类型点击事件  */
  searchTypeTap(e) {
    const item = e.currentTarget.dataset.item,
          index = e.currentTarget.dataset.index;
      this.setData({
          "listQuery.searchLabelId":item.value,
          typeIndex:index
      });
      this.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let that = this.data;
    if ((that.listQuery.pageNum + 1) <= that.total) {
      this.setData({
        "listQuery.pageNum": that.listQuery.pageNum += 1
      });
      this.getList();
    };
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})